<template>
	<view class="cart-container" v-if="cart.length !== 0">
		<!-- 选择收货地址 -->
		<my-address></my-address>
		<!-- 标题 -->
		<view class="cart-title">
			<uni-icons type="shop" size="20"></uni-icons>
			<text>购物车</text>
		</view>
		<!-- 商品列表 -->
		<!-- uni-swipe-action 是最外层包裹性质的容器 -->
		<uni-swipe-action>
			<block v-for="(item,index) in cart" :key="index">
				<!-- 注意 必须options指定是right还是left，right-options-->
				<uni-swipe-action-item :right-options="rightOptions" @click="swipeActionClickHandler(item)">

					<my-goods :goods="item" :showRadio="true" :showNum="true" @radio-change="radioChange"
						@change-num="changeNum"></my-goods>

				</uni-swipe-action-item>
			</block>
		</uni-swipe-action>

		<!-- 结算区域 -->
		<my-settle></my-settle>
	</view>

	<!-- 购物车为空 -->
	<view class="empty-cart" v-else>
		<image src="/static/empty.jpg" mode=""></image>
		<text>您的购物车里还没有商品~</text>
	</view>
</template>

<script>
	import badgeMixin from '@/mixins/tabbar-badge.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'

	export default {
		mixins: [badgeMixin],
		computed: {
			...mapState('my_cart', ['cart'])
		},
		data() {
			return {
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#C00000'
					}
				}]
			}
		},
		methods: {
			...mapMutations('my_cart', ['updateGoodsState', 'updateGoodsCount', 'romoveGoodsById']),
			// 更改商品选中状态
			radioChange(e) {
				// console.log(e)
				this.updateGoodsState(e)
			},
			// 更改商品数量
			changeNum(e) {
				// console.log(e);
				this.updateGoodsCount(e)
			},
			// 向右滑动删除商品
			swipeActionClickHandler(g) {
				// console.log(g);
				this.romoveGoodsById(g.goods_id)
			}
		}
	}
</script>

<style lang="scss">
	.cart-container {
		padding-bottom: 50px;

		.cart-title {
			height: 40px;
			// line-height: 40px;
			display: flex;
			align-items: center;
			font-size: 14px;
			padding-left: 5px;
			border-bottom: 1px solid #efefef;

			text {
				margin-left: 10px;
			}
		}
	}

	.empty-cart {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 200px;

		image {
			width: 115px;
			height: 90px;
		}

		text {
			font-size: 12px;
			color: gray;
			margin-top: 15px;
		}
	}
</style>
